package edu.mse.camel.client.posterboard.ui;

import org.eclipse.ui.IStartup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.events.EventIDs;
import edu.mse.camel.client.posterboard.ui.handlers.PBInitiationEventHandler;
import edu.mse.camel.client.posterboard.ui.handlers.ResourceUpdateHandler;

/**
 * The activator class controls the plug-in life cycle
 */
public class PosterboardPlugin extends AbstractUIPlugin implements IStartup {

	// The plug-in ID
	public static final String PLUGIN_ID = "edu.mse.camel.client.posterboard.ui";

	// The shared instance
	private static PosterboardPlugin plugin;
	
	private PBInitiationEventHandler pbInitiationHandler;
	
	private ResourceUpdateHandler resUpdHlr;
	/**
	 * The constructor
	 */
	public PosterboardPlugin() {
		pbInitiationHandler = new PBInitiationEventHandler();
		resUpdHlr = new ResourceUpdateHandler();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		//EventManager.getInstance().subscribeTo(EventIDs.RECEIVE_PEER_EVENT, handler);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		//EventManager.getInstance().unSubscribeTo(
				//EventIDs.RECEIVE_PEER_EVENT, handler);
		EventManager.getInstance().unSubscribeTo(
				EventIDs.POSTERBOARD_INITIATION_EVENT, pbInitiationHandler);
		EventManager.getInstance().unSubscribeTo(
				EventIDs.BASELINE_RESOURCE_UPDATE_EVENT, resUpdHlr);
		//handler = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static PosterboardPlugin getDefault() {
		return plugin;
	}

	@Override
	public void earlyStartup() {
		EventManager.getInstance().subscribeTo(
				EventIDs.POSTERBOARD_INITIATION_EVENT, pbInitiationHandler);
		EventManager.getInstance().subscribeTo(
				EventIDs.BASELINE_RESOURCE_UPDATE_EVENT, resUpdHlr);
	}

}
